Here are few examples of the enhanced for (for-each) loop in Java, along with classes and methods to illustrate their usage:
Example 1. Iterating Over an Array of Integers:
Iterating array using for each loop
public class Main{
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
Output
1 2 3 4 5
This example shows how to use the for-each loop to iterate over an array of integers and print each element.
Example 2. Iterating Over a List of Strings
Iterating string using for each loop
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
List<> fruits = new ArrayList<>(); //specify String for arraylist
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output
Apple
Banana
Orange
Here, we use the for-each loop to iterate over a list of strings and print each string.
Example 3. Iterating Over Characters in a String
Iterating characters in string using for each loop
public class Main{
public static void main(String[] args) {
String message = "Hello";
for (char character : message.toCharArray()) {
System.out.println(character);
}
}
}
Output
H
e
l
l
o
This example demonstrates using the for-each loop to iterate over characters in a string.
Example 4. Iterating Over an Array of Custom Objects
Iterating custom objects using for each loop
public class Main{
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class CustomObjectExample {
public static void main(String[] args) {
Student[] students = {new Student("Alice"), new Student("Bob"), new Student("Charlie")};
for (Student student : students) {
System.out.println(student.getName());
}
}
}
Output
Alice
Bob
Charlie
In this example, we use the for-each loop to iterate over an array of custom objects (Student) and access their attributes.
Example 5. Iterating Over a Collection of Objects
Iterating hashset using for each loop
import java.util.HashSet;
import java.util.Set;
public class Main{
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>(); //specify Integer
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
Output
1 2 3
Here, we use the for-each loop to iterate over a Set collection of integers.
Example 6. Iterating Over an Array of Multidimensional Arrays
Iterating multidimensional array using for each loop
public class Main{
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : matrix) {
for (int number : row) {
System.out.print(number + " ");
}
System.out.println();
}
}
}
Output
1 2 3
4 5 6
7 8 9
This example illustrates how to use the for-each loop with a multidimensional array.
Example 7. Iterating Over Enumerations
Iterating enum using for each loop
enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }
class Main{
public static void main(String[] args) {
for (DayOfWeek day : DayOfWeek.values()) {
System.out.println(day);
}
}
}
Output
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
Here, we use the for-each loop to iterate over an enumeration (enum).
Example 8. Iterating Over Files in a Directory
Iterating files using for each loop
//get the files from directory and prints the file name
import java.io.File;
class Main{
public static void main(String[] args) {
File directory = new File("/path/to/directory");
File[] files = directory.listFiles();
for (File file : files) {
System.out.println(file.getName());
}
}
}
This example demonstrates iterating over files in a directory using the for-each loop.